﻿namespace Apex.Commons.Extentions
{
    public static class RandomExtensions
    {
        /// <summary>
        /// 返回位于指定范围内的随机整数
        /// </summary>
        /// <param name="random"></param>
        /// <param name="minValue">要返回的随机数的包含下界</param>
        /// <param name="maxValue">要返回的随机数的不包含上界，maxValue 必须大于或等于 minValue</param>
        /// <returns>位于指定范围内的随机浮点数</returns>
        public static double NextDouble(this Random random, double minValue, double maxValue)
        {
            if (minValue >= maxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(minValue), "minValue 不能大于等于 maxValue");
            }
            // https://stackoverflow.com/questions/65900931/c-sharp-random-number-between-double-minvalue-and-double-maxvalue
            // 生成一个落在指定范围内的随机双精度浮点数
            double x = random.NextDouble();
            return x * maxValue + (1 - x) * minValue;
        }
    }
}